package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.*;
import com.tanhua.domain.vo.AnswersVo;
import com.tanhua.domain.vo.OptionsVo;
import com.tanhua.domain.vo.QuestionsVo;
import com.tanhua.domain.vo.SoulQuestionnaireVo;
import com.tanhua.dubbo.mapper.*;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SoulApiImpl implements SoulApi {
    @Autowired
    private SoulMapper soulMapper;
    @Autowired
    private QuestionnaireMapper questionnaireMapper;
    @Autowired
    private OptionMapper optionMapper;
    @Autowired
    private DimensionsMapper dimensionsMapper;
    @Autowired
    private ReportMapper reportMapper;

    @Override
    public List<SoulQuestionnaireVo> findQustion(Long loginUsrId) {
        List<SoulQuestionnaireVo> soulList = new ArrayList<>();
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        List<Questionnaire> questionnaires = questionnaireMapper.selectList(queryWrapper);
        for (Questionnaire questionnaire : questionnaires) {
            String questionId = questionnaire.getQuestionId();
            String[] splits = StringUtils.split(questionId, ',');
            SoulQuestionnaireVo<QuestionsVo> souls = new SoulQuestionnaireVo<>();
            //单套试卷的试题集合
            List<QuestionsVo> questionsVoList = new ArrayList<>();
            souls.setQuestions(questionsVoList);
            souls.setId(questionnaire.getId().toString());
            souls.setName(questionnaire.getName());
            souls.setCover(questionnaire.getCover());
            souls.setLevel(questionnaire.getLevel());
            souls.setStar(questionnaire.getStar());
            soulList.add(souls);
            for (int i = 0; i < splits.length; i++) {
                QuestionsVo<OptionsVo> options = new QuestionsVo<>();
                Map<String, Object> splitMap = new HashMap<String, Object>();
                splitMap.put("question_id", splits[i]);
                QueryWrapper<Questions> questionQueryWrapper = new QueryWrapper<>();
                questionQueryWrapper.eq("question_id", splits[i]).eq("option_id", 1);
                Questions questions = optionMapper.selectOne(questionQueryWrapper);
                //选项集合
                List<OptionsVo> optionsVos = new ArrayList<>();
                options.setOptions(optionsVos);
                options.setId(splits[i]);
                options.setQuestion(questions.getQuestion());
                List<Questions> QuestionList = optionMapper.selectByMap(splitMap);
                questionsVoList.add(options);
                for (Questions option : QuestionList) {
                    OptionsVo optionsVo = new OptionsVo();
                    optionsVo.setId(option.getOptionId().toString());
                    optionsVo.setOption(option.getOptions());
                    optionsVos.add(optionsVo);
                }
            }
        }
        QueryWrapper<Soul> soulQueryWrapper = new QueryWrapper<>();
        soulQueryWrapper.eq("user_id", loginUsrId);
        List<Soul> souls = soulMapper.selectList(soulQueryWrapper);
        if (CollectionUtils.isEmpty(souls)) {
            for (SoulQuestionnaireVo vo : soulList) {
                Soul soul = new Soul();
                if ("1".equals(vo.getId())){
                soul.setIsLock(0);
                }else {
                soul.setIsLock(1);
                }
                soul.setQuestionnaireId(Integer.valueOf(vo.getId()));
                soul.setUserId(loginUsrId.intValue());
                soulMapper.insert(soul);
            }
        }
        Map<Integer, Soul> soulMap = souls.stream().collect(Collectors.toMap(Soul::getQuestionnaireId, u -> u));
        for (SoulQuestionnaireVo vo : soulList) {
            Soul soul = soulMap.get(new Integer(vo.getId()));
            System.out.println(soul);
            if (null != soul) {
                if (null != soul.getReportId()) {
                    vo.setReportId(soul.getReportId().toString());
                }
                vo.setIsLock(soul.getIsLock());
            } else if ("1".equals(vo.getId())){
                vo.setIsLock(0);
            }
        }
        return soulList;
    }

    @Override
    public List<Questions> saveQuestion(AnswersVo answers) {
        List<Questions> questionsList = new ArrayList<>();
        List<Answer> answerList = answers.getAnswers();
        for (Answer answer : answerList) {
            Questions questions = optionMapper.findScore(answer.getQuestionId(), answer.getOptionId());
            questionsList.add(questions);
        }
        return questionsList;
    }

    @Override
    public void updateQuestion(AnswersVo answers, String reportId, Long userId) {
        List<Answer> answerList = answers.getAnswers();
        for (Answer answer : answerList) {
            System.out.println("输出了几次啊！！！！！！");
            Questions questions = optionMapper.findScore(answer.getQuestionId(), answer.getOptionId());
            Integer questionnaireId = questions.getQuestionnaireId();
            soulMapper.updateReport(questionnaireId, reportId, userId);
            soulMapper.updateIsLock(questionnaireId, userId);
            return;
        }

    }

    @Override
    public void updateDimensions(Dimensions dimensions) {
        QueryWrapper<Dimensions> dimensionsQueryWrapper = new QueryWrapper<>();
        dimensionsQueryWrapper.eq("user_id", dimensions.getUserId());
        Dimensions selectById = dimensionsMapper.selectOne(dimensionsQueryWrapper);
        if (null != selectById) {
            dimensionsMapper.update(dimensions, dimensionsQueryWrapper);
        } else {
            dimensionsMapper.insert(dimensions);
        }
    }

    @Override
    public List<Dimensions> findDimentsions() {
        QueryWrapper<Dimensions> dimensionsQueryWrapper = new QueryWrapper<>();
        List<Dimensions> dimensionsList = dimensionsMapper.selectList(dimensionsQueryWrapper);
        return dimensionsList;
    }

    @Override
    public Report findReport(String reportId) {
        Report report = reportMapper.selectById(reportId);
        return report;
    }
}
